import 'dart:io';

import 'package:get/get.dart';
import 'package:pet/app/modules/chat/providers/chat_provider.dart';

import '../../../websocket_controller.dart';

class ChatController extends GetxController {
  //TODO: Implement ChatController

  late WebsocketController websocketController;
  bool upload = false;
  @override
  void onInit() {
    super.onInit();
    websocketController = Get.find<WebsocketController>();
    websocketController.getShortcut();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    websocketController.currentId = '';
    websocketController.closeId = 0;
    websocketController.getClients();
    websocketController.messages = {};
  }

  getConversations(
      String id, String sendId, String type, String conversationId) async {
    print(type);
    try {
      if (type == 'online') {
        await websocketController.getConversations(id, sendId, conversationId);
      } else {
        await websocketController.getHistoryConversations(id, sendId);
      }
    } catch (e) {
      print(e);
    }
  }

  Future<dynamic> uploadImage(File file) async {
    ChatProvider chatProvider = Get.find<ChatProvider>();
    //执行网络请求
    Response response = await chatProvider.uploadImage(file);
    //请求出错时
    if (response.hasError) {
      return [];
    } else {
      return response.body['data'];
    }
  }

  uploading(bool status) {
    upload = status;
    update();
  }
}
